package com.link.base.base.subjectchangeapply.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.link.base.user.model.CoreUser;
import com.link.base.base.user.model.User;
import com.link.base.base.subjectchangeapply.model.SubjectChangeApply;
import com.link.base.base.subjectchangeapply.model.SubjectChangeApplyInfo;
import com.link.base.base.subjectchangeapply.service.SubjectChangeApplyService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liushoulong
 * @date 2018-11-08
 * @desc 信息变更申请
 */
@Controller("SubjectChangeApplyController")
@RequestMapping("/link/subjectChangeApply")
public class SubjectChangeApplyController extends BasicController<SubjectChangeApply> {

    @Resource
    private SubjectChangeApplyService subjectChangeApplyService;

    @Override
    public BasicService<SubjectChangeApply> getBasicService() throws Exception {
        return subjectChangeApplyService;
    }

    /**
     * 查询指定用户信息表更列表
     *
     * @param subjectChangeApply
     * @param qps
     * @return
     */
    @RequestMapping(value = "/queryByUserExamplePage")
    @ResponseBody
    public Map<String, Object> queryByUserExamplePage(@JsonParam SubjectChangeApply subjectChangeApply,@JsonParam  QueryParams qps) {
        Map<String, Object> result = new HashMap<String, Object>(8);
        try {
            CoreUser coreUser = UserUtil.getUser();
            User user = (User) coreUser;
            List<SubjectChangeApply> subjectChangeApplyList = subjectChangeApplyService.queryByUserExamplePage(user);
            result.put("rows", subjectChangeApplyList);
            result.put("success", true);

        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 提交申请，创建审批流程
     *
     * @param subjectChangeApplyInfo
     * @return
     */
    @RequestMapping(value = "/submit")
    @ResponseBody
    public Map<String, Object> submit(@RequestBody SubjectChangeApplyInfo subjectChangeApplyInfo) {
        Map<String, Object> result = new HashMap<>(8);
        ObjectMapper mapper = new ObjectMapper();
        try {
            String approvalDetail = mapper.writeValueAsString(subjectChangeApplyInfo);
            if (subjectChangeApplyInfo.getSubjectChangeApply().getId() == null) {
                throw new RuntimeException("id不能为空！");
            }
            subjectChangeApplyService.accntSigningSubmit(subjectChangeApplyInfo.getSubjectChangeApply(), approvalDetail);
            SubjectChangeApply newSub = new SubjectChangeApply();
            newSub = subjectChangeApplyService.queryById(subjectChangeApplyInfo.getSubjectChangeApply());
            result.put("newRow", newSub);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 更新企业认证图片
     *
     * @param entity
     */
    @RequestMapping("/updateCertificateImg")
    @ResponseBody
    public Map<String, Object> updateCertificateImg(@RequestBody SubjectChangeApply entity) {
        Map<String, Object> result = new HashMap<String, Object>(8);
        try {
            subjectChangeApplyService.updateCertificateImg(entity);
            SubjectChangeApply newRow = subjectChangeApplyService.queryById(entity);
            result.put("success", true);
            result.put("newRow", newRow);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
